/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Coursechapter;
import me.zhengjie.modules.system.domain.Coursechapterauditlog;
import me.zhengjie.modules.system.domain.Teacher;
import me.zhengjie.modules.system.domain.vo.CoursechapterauditlogQueryCriteria;
import me.zhengjie.modules.system.service.CoursechapterService;
import me.zhengjie.modules.system.service.CoursechapterauditlogService;
import me.zhengjie.modules.system.service.TeacherService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* @author shitou
* @date 2025-01-20
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程章节审核管理")
@RequestMapping("/api/coursechapterauditlog")
public class CoursechapterauditlogController {

    private final CoursechapterauditlogService coursechapterauditlogService;

    private final CoursechapterService coursechapterService;

    private final TeacherService teacherService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('coursechapterauditlog:list')")
    public void exportCoursechapterauditlog(HttpServletResponse response, CoursechapterauditlogQueryCriteria criteria) throws IOException {
        coursechapterauditlogService.download(coursechapterauditlogService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询课程章节审核")
    @ApiOperation("查询课程章节审核")
//    @PreAuthorize("@el.check('coursechapterauditlog:list')")
    public ResponseEntity<List<Coursechapterauditlog>> queryCoursechapterauditlog(CoursechapterauditlogQueryCriteria criteria){

        List<Coursechapterauditlog> coursechapterauditlogList = coursechapterauditlogService.queryAll(criteria);
        List<Integer> courseChapterIds = new ArrayList<>();
        List<Integer> teacherIds = new ArrayList<>();
        for(Coursechapterauditlog coursechapterauditlog:coursechapterauditlogList){
            courseChapterIds.add(coursechapterauditlog.getCourseChapterId());
            teacherIds.add(coursechapterauditlog.getTeacherId());
        }
        if(courseChapterIds.size()!=0){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",courseChapterIds);
            List<Coursechapter> coursechapterList = coursechapterService.list(queryWrapper);
            System.out.println("==coursechapterList=="+coursechapterList);
            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.in("id",teacherIds);
            List<Teacher> teacherList = teacherService.list(queryWrapper1);
            System.out.println("==teacherList=="+teacherList);
            for(Coursechapterauditlog coursechapterauditlog:coursechapterauditlogList){
                for(Coursechapter coursechapter:coursechapterList){
                    if(coursechapter.getId() == coursechapterauditlog.getCourseChapterId()){
                        coursechapterauditlog.setCourseChpaterName(coursechapter.getName());
                    }
                }

                for(Teacher teacher:teacherList){
                    if(Math.toIntExact(teacher.getId()) == coursechapterauditlog.getTeacherId()){
                        coursechapterauditlog.setTeacherName(teacher.getNickName());
                    }
                }
            }
        }
        return new ResponseEntity<>(coursechapterauditlogList,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增课程章节审核")
    @ApiOperation("新增课程章节审核")
//    @PreAuthorize("@el.check('coursechapterauditlog:add')")
    public ResponseEntity<Object> createCoursechapterauditlog(@Validated @RequestBody Coursechapterauditlog resources){
        coursechapterauditlogService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改课程章节审核")
    @ApiOperation("修改课程章节审核")
//    @PreAuthorize("@el.check('coursechapterauditlog:edit')")
    public ResponseEntity<Object> updateCoursechapterauditlog(@Validated @RequestBody Coursechapterauditlog resources){
        coursechapterauditlogService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除课程章节审核")
    @ApiOperation("删除课程章节审核")
//    @PreAuthorize("@el.check('coursechapterauditlog:del')")
    public ResponseEntity<Object> deleteCoursechapterauditlog(@RequestBody List<Integer> ids) {
        coursechapterauditlogService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}